//
//  PopupMenuView.m
//  etionUI
//
//  Created by wangjian on 11/29/13.
//  Copyright (c) 2013 GuangZhouXuanWu. All rights reserved.
//

#import "LoadAddressPopupView.h"

#define MARGIN 15
#define RADIUS 5
#define ARROR_SIZE 7

#define MENU_TITLE_H 40
#define MENU_TITLE_FONT [UIFont systemFontOfSize:14]


@interface LoadAddressPopupView ()

@end

@implementation LoadAddressPopupView
{
}

- (void)dealloc
{
}

- (id)initWithAnchorView:(UIView *)anchorView parnetView:(UIView*)parentView
{
    self = [super initWithParentView:parentView];
    
    self.anchorView = anchorView;
    
    self.arrorDirection = EPopupViewArrorDirBottom;
    
    return self;
}

- (void)setAddress:(NSString*)address
{
    CGSize size = [address wrapStringSizeWithFont:[UIFont systemFontOfSize:14] forWidth:200];
    UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    textLabel.text = address;
    textLabel.font = [UIFont systemFontOfSize:14];
    textLabel.textColor = [UIColor whiteColor];
    textLabel.numberOfLines = 0;
    textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    textLabel.backgroundColor = [UIColor clearColor];
    
    UIView *containView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, size.width + 10, MAX(35,size.height + 12))];
    textLabel.center = CGPointMake(containView.width/2, containView.height/2 - 3);
    [containView addSubview:textLabel];
    
    [self setView:containView];
}

- (void)initialize
{
    self.viewBackgroundColor = [UIColor colorWithR:0 G:0 B:0 A:0.7];
    UIView *containView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 35)];
    
    UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [act startAnimating];
    act.center = CGPointMake(containView.width/2, containView.height/2 - 2);
    [containView addSubview:act];
    
    [self setView:containView];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    return NO;
}

- (void)show
{
    [self initialize];

    [self.parentView addSubview:self];
    _contentView.alpha = 0.0;
    
    self.userInteractionEnabled = NO;
    _contentView.frame = CGRectOffset(_contentView.frame, 0, _arrorDirection == EPopupViewArrorDirTop ? -MARGIN : MARGIN);
    [UIView animateWithDuration:.25 animations:^
    {
        _contentView.alpha = 1.0;
        _contentView.frame = CGRectOffset(_contentView.frame, 0, _arrorDirection == EPopupViewArrorDirTop ? MARGIN : -MARGIN);
    } completion:^(BOOL finished)
    {
        self.userInteractionEnabled = YES;
    }];
}

- (void)dismiss
{
    self.userInteractionEnabled = NO;
    [UIView animateWithDuration:.25 animations:^
    {
        _contentView.alpha = 0.0;
        _contentView.frame = CGRectOffset(_contentView.frame, 0, _arrorDirection == EPopupViewArrorDirTop ? -MARGIN : MARGIN);
    } completion:^(BOOL finished)
    {
        self.userInteractionEnabled = YES;
        _contentView.frame = CGRectOffset(_contentView.frame, 0, _arrorDirection == EPopupViewArrorDirTop ? MARGIN : -MARGIN);
        [self removeFromSuperview];
    }];
}

@end
